package com.zh.common.util;

import com.sun.media.jfxmediaimpl.HostUtils;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * code is far away from bug with the animal protecting
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * @Description: 时间操作工具类
 * @Author: Li.HuiNeng
 * @Create in: 2019/8/24
 * @Version: 1.0.1
 */
public class DateSetUtil {

    /**
     * @Description: date的那月第一天
     * ------------------------
     * @Author: Li.HuiNeng
     * @Date: 2019/8/24
     */
    public static Date getMonthFirstDay(Date date) {
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getMinimum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        //设置当前时刻的分钟为0
        calendar.set(Calendar.MINUTE, 0);
        //设置当前时刻的秒钟为0
        calendar.set(Calendar.SECOND, 0);
        //设置当前的毫秒钟为0
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * @Description: date的那月最后一天
     * ------------------------
     * @Author: Li.HuiNeng
     * @Date: 2019/8/24
     */
    public static Date getMonthLastDay(Date date) {
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        //设置当前时刻的分钟为0
        calendar.set(Calendar.MINUTE, 59);
        //设置当前时刻的秒钟为0
        calendar.set(Calendar.SECOND, 59);
        //设置当前的毫秒钟为0
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTime();
    }

    /**
     * @Description: 当天0时0分0秒
     * ------------------------
     * @Author: Li.HuiNeng
     * @Date: 2019/8/24
     */
    public static Date getDayFirstSec(Date date) {
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        //设置当前时刻的分钟为0
        calendar.set(Calendar.MINUTE, 0);
        //设置当前时刻的秒钟为0
        calendar.set(Calendar.SECOND, 0);
        //设置当前的毫秒钟为0
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * @Description: 当天23时59分59秒
     * ------------------------
     * @Author: Li.HuiNeng
     * @Date: 2019/8/24
     */
    public static Date getDayLastSec(Date date) {
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        //设置当前时刻的分钟为0
        calendar.set(Calendar.MINUTE, 59);
        //设置当前时刻的秒钟为0
        calendar.set(Calendar.SECOND, 59);
        //设置当前的毫秒钟为0
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTime();
    }


    /**
     * @Description: 获取周一时间
     * ------------------------
     * @Author: Li.HuiNeng
     * @Date: 2019/8/24
     */
    public static Date getMondayOfThisWeek(Date date) {
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.setTime(date);
        int day_of_week = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (day_of_week == 0) {
            day_of_week = 7;
        }
        calendar.add(Calendar.DATE, - day_of_week + 1);
        return calendar.getTime();
    }

    /**
     * @Description: 获取周日时间
     * ------------------------
     * @Author: Li.HuiNeng
     * @Date: 2019/8/24
     */
    public static Date getWeekEndTime(Date date) {
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        int day_of_week = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (day_of_week == 0) {
            day_of_week = 7;
        }
        calendar.add(Calendar.DATE, -day_of_week + 7);
        return calendar.getTime();
    }


    /**
     * @Description: 获取本月第一天0点时间
     * ------------------------
     * @Author: Li.HuiNeng
     * @Date: 2019/8/24
     */
    public static String getFirstDayOfMonth() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(cal.getTime());
    }


    /**
     * @Description: 获得本月最后一天24点时间
     * ------------------------
     * @Author: Li.HuiNeng
     * @Date: 2019/8/24
     */
    public static String getLastDayOfMonth() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        cal.set(Calendar.HOUR_OF_DAY, 24);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(cal.getTime());
    }


    /**
     * @Description: 获取当年的第一天
     * ------------------------
     * @Author: Li.HuiNeng
     * @Date: 2019/8/24
     */
    public static Date getCurrYearFirst() {
        Calendar currCal = Calendar.getInstance();
        int currentYear = currCal.get(Calendar.YEAR);
        return getYearFirst(currentYear);
    }

    /**
     * @Description: 获取当年的最后一天
     * ------------------------
     * @Author: Li.HuiNeng
     * @Date: 2019/8/24
     */
    public static Date getCurrYearLast() {
        Calendar currCal = Calendar.getInstance();
        int currentYear = currCal.get(Calendar.YEAR);
        return getYearLast(currentYear);
    }

    /**
     * @Description: 获取某年第一天日期
     * ------------------------
     * @Author: Li.HuiNeng
     * @Date: 2019/8/24
     */
    public static Date getYearFirst(int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        Date currYearFirst = calendar.getTime();
        return currYearFirst;
    }

    /**
     * @Description: 获取某年最后一天日期
     * ------------------------
     * @Author: Li.HuiNeng
     * @Date: 2019/8/24
     */
    public static Date getYearLast(int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.roll(Calendar.DAY_OF_YEAR, -1);
        Date currYearLast = calendar.getTime();
        return currYearLast;
    }

    /**
     * @Description: 获取当前时间指定前几天的日期
     * ------------------------
     * @Author: Li.HuiNeng
     * @Date: 2020/7/11
     */
    public static String getDateByNum(int number) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.set(Calendar.DATE, (calendar.get(Calendar.DATE) - number));
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        //设置当前时刻的分钟为0
        calendar.set(Calendar.MINUTE, 0);
        //设置当前时刻的秒钟为0
        calendar.set(Calendar.SECOND, 0);
        //设置当前的毫秒钟为0
        calendar.set(Calendar.MILLISECOND, 0);
        return  sdf.format(calendar.getTime());
    }
}
